# ====================================
# Company: Zonge International, INC.
# Developer: Wanjie Feng
# Date: 5/4/2023
# Time: 11:07 AM
# Filename: Read_Files_In_Folder.py
# IDE: PyCharm
# =====================================
"""
    This code is to open a directory and read single *.cvs file in the directory.
"""
import os
import tkinter as tk
from tkinter import filedialog

def Read_File(Title_Info):
    """This code is to open a directory and read single *.cvs file in the directory.
        return fileloc
    """

    CurrentDir = os.path.dirname(os.path.abspath(__file__))     # current direcotry
    print(CurrentDir)
    #CurrentDir = os.getcwd()
    os.chdir(CurrentDir)
    try:
        tmp = os.path.exists('path')        #check if previous path saved
        if tmp:
            f = open("path", "r")
            FolderLoc = f.read()            #read Folder location
            f.close()
        else:
            FolderLoc = CurrentDir          #current directory
        root = tk.Tk()
        root.withdraw()
        #root.iconbitmap(r'splash.ico')     #company log
        filetypes = (
            ('Choose '+ Title_Info, '*.cvs'),
        )
        IntFile = ' '                       #pre-defined filename
        FolderLoc = filedialog.askopenfilename(parent=root, initialdir=FolderLoc, \
                                            title=Title_Info,filetypes=filetypes,\
                                            initialfile=IntFile)

        f = open("path", "w+")      #save Folder directory to path file
        f.write(FolderLoc)
        f.close()
        #print(FolderLoc)
    except Exception as e:
        print(e)

    return FolderLoc

if __name__ == '__main__':
    Info = "Read cvs file"
    filename = Read_File(Info)
    print(filename)
